package com.example.demo.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.CmsArticle;
import com.example.demo.common.Response;
import com.example.demo.service.CmsArticleService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/article")
public class CmsArticleController {
	
	@Autowired
	private CmsArticleService cmsArticleService;
	
	@GetMapping("/selectall")
	@ApiOperation(value = "获取所有文章信息" , notes = "从数据库检索所有文章信息并返回")
	public Response< List<CmsArticle>> quaryAllArticle()
	{
		List<CmsArticle> cs =  cmsArticleService.getAllArticles();
		return Response.ok(cs);
	}
		
	@ApiOperation(value = "审核文章")
	@ApiImplicitParams({
		@ApiImplicitParam(name="artId",value = "文章编号"),
		@ApiImplicitParam(name = "state",value = "状态",
							allowableValues = "已发布,已驳回")
	})
	@PutMapping(value = "/updateState")
	public Response<String> updateState(String artId,
			String state){
		boolean b = cmsArticleService.updateState(artId, state);
		if(b) return Response.ok("操作成功！");
		else return Response.error("要更新的文章不存在！");
	}
	
	
	
	@ApiOperation(value = "保存或者更新文章信息" ,notes = "如果传递参数时指明了栏目编号，那么执行更新操作<br/>"+
	"如果传递参数时没有指明栏目编号，那么执行插入操作")
	@PostMapping("/saveorupdate")
	public Response<String> saveOrUpdate(CmsArticle cmsArticle)
	{
		if(cmsArticleService.saveOrUpdateCmsArticle(cmsArticle))
		{
			return Response.ok("操作成功");
		}
		return Response.error("操作失败");
		
	}
	
	@ApiOperation(value = "保存或者更新文章信息" , 
			notes = "如果给定文章编号，那么代表更新文章信息<br/>"
					+ "如果没有给定文章编号，那么代表新增文章信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",
				value = "文章编号,更新时必须传递"),
		@ApiImplicitParam(name = "title",value = "文章标题"),
		@ApiImplicitParam(name = "author",value = "文章作者"),
		@ApiImplicitParam(name = "categoryId",value = "所属栏目编号"),
	})
	@PostMapping("/saveorupdate2")
	public Response<String> saveOrUpdate2(String id,String title,String author,String categoryId)
	{
	  if (	cmsArticleService.saveOrUpdate(id, title, author, categoryId)) {
		  return Response.ok("操作成功！");
	}
		return Response.error("操作失败！");
	}
	
		
		@ApiOperation(value = "按照条件检索文章信息")
		@ApiImplicitParams({
			@ApiImplicitParam(name = "begin",value = "开始时间"),
			@ApiImplicitParam(name = "end",value = "结束时间"),
			@ApiImplicitParam(name = "key",value = "文章标题或者作者")
		})
		@GetMapping("/searchBy")
		public Response<List<CmsArticle>> searchBy(
				Date begin,Date end,String key){
			System.out.println("begin:"+begin);
			System.out.println("end:"+end);
			List<CmsArticle> as = 
					cmsArticleService.searchBy(begin, end, key);
			return Response.ok(as);
		}
		
		@DeleteMapping("/deleteById")
		public Response<String> deleteById(String id) {
			int count = cmsArticleService.deleteById(id);
			if (count==-1) {
				return Response.error("文章不存在！！！");
			}else if (count ==-2) {
				return Response.error("父栏目不存在！！！");
			}
			
			return Response.ok("删除成功！！！");
		}

}
